$(function(){
	
	$("#head").load("head.html?_="+Math.random())
	$("#fooster").load("fooster.html?_="+Math.random())
	
	var n = 0;
	$(".list_classify ul li button").click(function(){//左边打开列表详情
		console.log(this)
		if( n == 0){
			$(this).html("-").closest("li").next(".list_classify_box").show();
			n = 1;
		}else{
			$(this).html("+").closest("li").next(".list_classify_box").hide();
			n = 0;
		}
	})
	
	$(".week_hot .hot_title input").mouseover(function(){
		$(".week_hot .hot_title input").css("background","#ccc").eq($(this).index()).css("background","red");
		$("#hot_classify ._sail").hide().eq($(this).index()).show();
	})
	
	
	$.ajax({
		type:"get",
		url:"libs/JSON/ajax.json",
		async:true,
		success:function(obj){
			//请求列表
			var page = 1//页码
				creat()
			function creat(){
				$("#pro_list").empty()
				for(i = 12*page - 12 ;i< 12*page ;i++){//请求添加数据
					var html = ""
					html = "<div class='goods'>"
					html += "<img src='" + obj.data[i].img + "'/>"
					html += "<p><input type='checkbox' name='selected' id='selected'/>" + obj.data[i].name + "</p>"
					html += "<input type='button' name='compare' id='compare' value='对比所选商品' />"
					html += "<p id='price'>￥" + obj.data[i].price + "</p>"
					html +=	"<p><img src='img/product_detail/assess.png'/><a href='#'>（"+ obj.data[i].comment +"条评论）</a></p>"
					html +=	"<p><input type='button' name='' class='buy' value='快速购买' /><input type='button' name='' class='add_cart' value='加入购物车' /></p>"
					html += "</div>"
					$("#pro_list").append(html);
				}
			}
			//列表产品详情查看
			$(".goods").click(function(){
				index = $(this).index()
				//传参
				
				var name = escape(obj.data[index].name); 
				window.open( "product_detail.html"+"?name="+ name + "&img=" + obj.data[index].img + "&price=" + obj.data[index].price + "&comment=" + obj.data[index].comment)
			})
			
			
			//$("input:[type = checkbox]").attr("checked","checked");
			
			//列表购买
			$(".add_cart").click(function(event){
				
				console.log(event.currentTarget)
				var index = $(this).closest(".goods").index()
				var pro = getCookie("pro");
				var	obj0 = {
						"name":obj.data[index].name,
						"img":obj.data[index].img,
						"price":obj.data[index].price ,
						"comment":obj.data[index].comment
					}
				if(pro){ //购物车不为空
					var  pro = JSON.parse(getCookie("pro"))
					var d = new Date();
					d.setDate(d.getDate()+10);
					pro.data.push(obj0)
					cart_pro = JSON.stringify(pro)
					setCookie("pro",cart_pro,d)
					console.log(JSON.parse(getCookie("pro")))
				}else{  //购物车是空
					var pro = {data:[]}
					var d = new Date();
					d.setDate(d.getDate()+10);
					pro.data.push(obj0)
					cart_pro = JSON.stringify(pro)
					setCookie("pro",cart_pro,d)
					console.log(cart_pro)
				}
					
				$("#add_cart").show() //重新获取cookie
				var pro = getCookie("pro");
				var pro = JSON.parse(getCookie("pro"))
				$("#cart_mount").html(pro.data.length) //显示购物车数量
				var total_price = 0;
				for(var j=0;j<pro.data.length;j++){
					total_price +=parseInt( pro.data[j].price )
				}
				$("#price").html(total_price)
				
				event.stopPropagation();
				return false
			})
			
			
			
			$("#close").click(function(){
				$("#add_cart").hide()
			})
			
			
			
			//翻页
			$("#change_page li").mouseover(function(){
				$("#change_page li").css("background","white")
				$(this).css({"background":"#666"})
			}).mouseout(function(){
				$(this).css("background","white")
			}).click(function(){
				if($(this).index() == 0 ){//上翻
						page = page - 1;
						if(page == 0){
							page = 4;
						}
						creat()
				}else if($(this).index() == 5){//下翻
						page = page + 1;
						if(page == 5){
							page = 1;
						}
						creat()
				}else{//选择页码翻页
					page = parseInt($(this).html())
					creat()
				}
			})
			
		},
		error:function(){
			console.log("数据请求出错")
		}
	})
	
})